﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BaseHelper.Encrypt;
using BaseHelper.Text;

namespace DeviceSDK.LED.NuoWa.JT100
{
    public class PlayBill
    {

        private Encoding _encoding = Encoding.UTF8;
        private List<List<object>> _ltPage = new List<List<object>>();
        private List<object> _ltItem;
       
        //停留时间100ms
        //[all]
        //item=总条目数
        //[item1]
        //param=停留时间,入屏方式,出屏方式,入屏速度,闪烁速度,闪烁次数,播放次数
        //img1=x 坐标,y 坐标,文件名称,闪烁,显示区域宽度,显示区域高度
        //imgparam1=停留时间
        //imgN=x 坐标,y 坐标,文件名称,闪烁,显示区域宽度,显示区域高度
        //imgparamN=停留时间
        //txt1=x 坐标,y 坐标,字体,字号,前景颜色,背景颜色,闪烁,文字内容,显示
        //区域宽度,显示区域高度,字体风格
        //txtparam1=字符间距,排列方式，滚动方向
        //……
        //txtN=x 坐标,y 坐标,字体,字号,前景颜色,背景颜色,闪烁,文字内容,显示
        //区域宽度,显示区域高度,字体风格
        //txtparamN=字符间距,排列方式，滚动方向
        //video1=x 坐标,y 坐标,显示区域宽度,显示区域高度,文件名称,播放次数
        //……
        //videoN=x 坐标,y 坐标,显示区域宽度,显示区域高度,文件名称,播放次数
        //gif1=x 坐标,y 坐标,显示区域宽度,显示区域高度,文件名称,播放时长,播放次数,背景颜色
        //……
        //gifN =x 坐标,y 坐标,显示区域宽度,显示区域高度,文件名称,播放时长,播放次数,背景颜色
        //time1=x 坐标,y 坐标,显示区域宽度,显示区域高度，播放时长,前景颜色，背景颜色，字号,字体,字体风格,是否显示四位年,日期显示格式,是否显示年,月,日,时,分,秒,星期,是否单行显示,播放次数
        //timeN=x 坐标,y 坐标,显示区域宽度,显示区域高度，播放时长,前景颜色，背景颜色，字号,字体,字体风格,是否显示四位年,日期显示格式,是否显示年,月,日,时,分,秒,星期,是否单行显示,播放次数
        //txtext1=x 坐标,y 坐标,显示区域宽度,显示区域高度,字体,字号,字体风格,水平对齐,垂直对齐,行距,字距,前景颜色,背景颜色,播放特效,特效速度,播放时长,播放次数,文本内容
        //……
        //txtextN=x 坐标,y 坐标,显示区域宽度,显示区域高度,字体,字号,字体风格,水平对齐,垂直对齐,行距,字距,前景颜色,背景颜色,播放特效,特效速度,播放时长,播放次数,文本内容

        public IEnumerable<object> Items
        {
            get {

                for (int i = 0; i < _ltPage.Count; i++)
                {
                    for (int j = 0; j < _ltPage[i].Count; j++)
                    {
                        if (_ltPage[i][j] is ImageItem || _ltPage[i][j] is VideoItem || _ltPage[i][j] is GifItem)
                        {
                            yield return _ltPage[i][j];
                        }
                    }
                
                }
            }
        }

        public int Count
        {
            get { return _ltItem.Count; }
        }

        public void Clear()
        {
            _ltItem.Clear();
        }

        /// <summary>
        /// 增加页面
        /// </summary>
        /// <param name="PlayTime">单位：100ms</param>
        /// <param name="InEffect">0</param>
        /// <param name="OutEffect">0</param>
        public void AddPage(int PlayTime,int InEffect,int OutEffect)
        {
            ItemEffect item = new ItemEffect();
            item.PlayTime = PlayTime;
            item.InEffect = InEffect;
            item.OutEffect = OutEffect;
            _ltItem = new List<object>();
            _ltItem.Add(item);
            _ltPage.Add(_ltItem);
          
        }

        public void RemoveLastPage()
        {
            if(_ltPage.Count>0)
                _ltPage.RemoveAt(_ltPage.Count - 1);
        }

        public void AddImage(string FileName,int PlayTime,int X,int Y,int Width,int Height)
        {
            FileInfo file = new FileInfo(FileName);
            string fileMd5 = Md5Helper.Encrypt(FileName);
            string newFileName =fileMd5 +"_" + fileMd5 +  file.Extension.ToLower();

            ImageItem img = new ImageItem();
            img.FileName = newFileName;
            img.FilePath = FileName;
            img.PlayTime=PlayTime;
            img.X=X;
            img.Y=Y;
            img.Width=Width;
            img.Height=Height;
            _ltPage[_ltPage.Count-1].Add(img);        
        }

        public void AddVideo(string FileName, int PlayTime, int X, int Y, int Width, int Height)
        {
            FileInfo file = new FileInfo(FileName);
            string newFileName = Md5Helper.Encrypt(FileName) + file.Extension.ToLower();


            VideoItem video = new VideoItem();
            video.FileName = newFileName;
            video.FilePath = FileName;
            video.PlayCount = 1;
            video.PlayTime = PlayTime;
            video.X = X;
            video.Y = Y;
            video.Width = Width;
            video.Height = Height;
            _ltPage[_ltPage.Count - 1].Add(video);
        }

        public void AddText2(string Text, int X, int Y, int Width, int Height, string FontName, int FontSize, string FontColor = "1",  int FontStyle = 0, int FontSpace = 0, int FontDirection = 0)
        { 

            TextItem txt=new TextItem();
            txt.Text=TextFormat(Text);
            txt.X=X;
            txt.Y=Y;
            txt.Width=Width;
            txt.Height=Height;
            txt.FontName=(FontName);
            //txt.FontSize=FotnSize;
            txt.FontColor=(FontColor);
            txt.BackColor = "8"; // ColorFormat(BackColor);
            txt.FontStyle=FontStyle;
            txt.FontSpace=FontSpace;
            txt.FontDirection=FontDirection;

            txt.FontSize = FontSize <= 1 ? TextFontSizeHelper.CalcFontSize(Text, Width, Height) : FontSize;


            _ltPage[_ltPage.Count - 1].Add(txt);
        }

        //playTime=100ms，单位是100ms
        public void   AddString(string Text,int X,int Y,int Width,int Height,string FontName,int FontSize,int FontStyle=0,int AlignX=0,int AlignY=0,int LineHeight=1,int FontSpace=0,string FontColor="1",int Effect=0,int EffectSpeed=2,int PlayCount=1,int PlayTime=100)
        {
            StringItem str=new StringItem();
            str.Text=TextFormat(Text);
            str.X=X;
            str.Y=Y;
            str.Width=Width;
            str.Height=Height;
            str.FontName = FontNameFormat(FontName);
            //str.FontSize=FontSize;
            str.FontStyle=FontStyle;
            str.AlignX=AlignX;
            str.AlignY=AlignY;
            str.LineHeight=LineHeight;
            str.FontSpace=FontSpace;
            str.FontColor = ColorFormat(FontColor);
            str.BackColor ="8";
            str.Effect=Effect;
            str.EffectSpeed=EffectSpeed;
            str.PlayCount=PlayCount;
            str.PlayTime=PlayTime;

            str.FontSize = FontSize <= 1 ? TextFontSizeHelper.CalcFontSize(Text,Width, Height) : FontSize;

            _ltPage[_ltPage.Count - 1].Add(str);
        }

        public void AddGif(string FileName,int X,int Y,int Width,int Height,int PlayTime=100,int PlayCount=1,string BackColor="8")
        {
            FileInfo file = new FileInfo(FileName);
            string newFileName = Md5Helper.Encrypt(FileName) + file.Extension.ToLower();

            GifItem gif = new GifItem();
            gif.FileName = newFileName;
            gif.FilePath = FileName;
            gif.X = X;
            gif.Y = Y;
            gif.Width = Width;
            gif.Height = Height;
            gif.PlayTime = PlayTime;
            gif.PlayCount = PlayCount;
            gif.BackColor = BackColor;
            _ltPage[_ltPage.Count - 1].Add(gif);
        }

        public void RemoveLastItem()
        {
            if (_ltPage[_ltPage.Count - 1].Count > 0)
                _ltPage[_ltPage.Count - 1].RemoveAt(_ltPage[_ltPage.Count - 1].Count - 1);
        }

        private string TextFormat(string Text)
        {
            return Text.Replace("\\", "\\\\").Replace(",", "\\,").Replace("=", "\\=").Replace("\r\n", "\\r\\n"); ;
        }


        private string ColorFormat(string ColorHex)
        { 
            switch(ColorHex)
            {
                case "#00FF00":return "2";
                case "#FF0000":return "1";
                case "#FFFF00":return "4";
            }
            
            return "4";
        }

        private string FontNameFormat(string Font)
        {
            switch (Font)
            {
                case "宋体": return "3";
                case "仿宋": return "4";
                case "楷体":  return "2";
                case "黑体": return "1";

            }
            return "3";
        }

        public string Create()
        {
            //if (File.Exists(FilePath))
            //    File.Delete(FilePath);

            string output = "[all]\r\n";
            output += "items=" + _ltPage.Count.ToString() + "\r\n";


            int imgCount = 1;
            int videoCount = 1;
            int textCount = 1;
            int gifCount=1;
            int strCount=1;

            for (int i = 0; i < _ltPage.Count; i++)
            {
                output += "[item" + (i+1).ToString() + "]\r\n";

                for (int j = 0; j < _ltPage[i].Count; j++)
                {
                    object item = _ltPage[i][j];
                    if (item is ItemEffect)
                    {
                        ItemEffect eff=(ItemEffect)item;
                        output += "param=" + eff.PlayTime.ToString() + "," + eff.InEffect.ToString() + "," + eff.OutEffect.ToString() + "," + eff.InSpeed.ToString() + "," + eff.FlashSpeed.ToString()+"," + eff.FlashTimes + "," + eff.PlayCount.ToString() + "\r\n";
                    }
                    else if (item is ImageItem)
                    {
                        //img1=x 坐标,y 坐标,文件名称,闪烁,显示区域宽度,显示区域高度
                        //imgparam1=停留时间

                        ImageItem img = (ImageItem)item;
                        output += "img" + imgCount.ToString() + "=" + img.X.ToString() + "," + img.Y.ToString() + "," + img.FileName + "," + img.Flash + "," + img.Width + "," + img.Height + "\r\n";
                        output += "imgparam" + imgCount.ToString() + "=" + img.PlayTime.ToString() + "\r\n";
                        imgCount++;
                    }
                    else if (item is VideoItem)
                    {
                        //video1=x 坐标,y 坐标,显示区域宽度,显示区域高度,文件名称,播放次数

                        VideoItem video = (VideoItem)item;
                        output += "video" + videoCount.ToString() + "=" + video.X.ToString() + "," + video.Y.ToString() + "," + video.Width + "," + video.Height + "," + video.FileName + "," + video.PlayCount.ToString() + "\r\n";
                        videoCount++;
                    }
                    else if (item is TextItem)
                    {
                        //txt1=x 坐标,y 坐标,字体,字号,前景颜色,背景颜色,闪烁,文字内容,显示区域宽度,显示区域高度,字体风格
                        //txtparam1=字符间距,排列方式，滚动方向

                        TextItem txt = (TextItem)item;
                        output += "txt" + textCount.ToString() + "=" + txt.X.ToString() + "," + txt.Y.ToString() + "," + txt.FontName + "," + txt.FontSize.ToString() + txt.FontSize.ToString() +  "," + txt.FontColor + "," + txt.BackColor + "," + txt.Flash + "," + txt.Text + "," + txt.Width.ToString() + "," + txt.Height.ToString() + "," + txt.FontStyle.ToString() + "\r\n";
                        output += "txtparam" + textCount.ToString() + "=" + txt.FontSpace + "," + txt.FontDirection.ToString() + "," + txt.FlashDirection.ToString() + "\r\n";
                        textCount++;
                    }
                    else if (item is GifItem)
                    {
                        //gifN =x 坐标,y 坐标,显示区域宽度,显示区域高度,文件名称,播放时长,播放次数,背景颜色

                        GifItem gif = (GifItem)item;
                        output += "gif" + gifCount.ToString() + "=" + gif.X.ToString() + "," + gif.Y.ToString() + "," + gif.Width.ToString() + "," + gif.Height.ToString() + "," + gif.FileName + "," + gif.PlayTime.ToString() + "," + gif.PlayCount.ToString() + "," + gif.BackColor + "\r\n";
                        gifCount++;

                    }
                    else if (item is StringItem)
                    {
                        //txtext1=x 坐标,y 坐标,显示区域宽度,显示区域高度,字体,字号,字体风格,水平对齐,垂直对齐,行距,字距,前景颜色,背景颜色,播放特效,特效速度,播放时长,播放次数,文本内容
                        StringItem str = (StringItem)item;
                        output += "txtext" + strCount.ToString() + "=" + str.X.ToString() + "," + str.Y.ToString() + "," + str.Width.ToString() + "," + str.Height.ToString() + "," + str.FontName + "," + str.FontSize.ToString() + str.FontSize.ToString() + "," + str.FontStyle.ToString()
                            + "," + str.AlignX.ToString() + "," + str.AlignY.ToString() + "," + str.LineHeight.ToString() + "," + str.FontSpace.ToString() + "," + str.FontColor + "," + str.BackColor + "," + str.Effect.ToString() + "," + str.EffectSpeed.ToString() + "," + str.PlayTime.ToString() + "," + str.PlayCount.ToString() + "," + str.Text + "\r\n";
                        strCount++;
                    }
                }
            }

            return output;

        }

       
    }

    class ItemBase
    {
        public int X = 0;
        public int Y = 0;
        public int Width = 0;
        public int Height = 0;


        public int PlayCount = 1;   //1-255
        public int PlayTime = 100;    //0.1s 默认      


        public string Flash = "0";
        public string FileName = "";
        public string FilePath = "";
    }

    class ItemEffect
    {
        public int PlayCount = 1;
        public int PlayTime=100;    //0.1s 默认      
        public int InEffect = 1;    //1-立即显示 2-上展 3-下展 4-左展 5-右展 6-上移 7-下移 8-左移 9-右移 10－淡入（入屏）淡出（出屏）255-随机
        public int OutEffect = 1;
        public int InSpeed = 1;
        public int OutSpeed = 1;
        public int FlashSpeed = 0;
        public int FlashTimes = 5;
    }


    class StringItem : ItemBase
    {
        public string FontName = "1";    //1-黑 2-楷 3-宋 4-仿宋 5-隶书，也可以是字体名称（Arial 等）    
        public int FontSize = 16;//"1616";    //各占两个字符。如：0909、1616、3232等
        public int FontStyle = 0;   //0-常规，1-加粗，2-倾斜，4-下划线，8-中划线（特效可以组合，如 3 表示字体加粗并且倾斜）
        public int AlignX = 0;  //0-左 1-右 2-中
        public int AlignY = 0;  //0-上 1-中 2-下
        public int LineHeight = 1;  //0-100
        public int FontSpace = 0;   //0-100
        public string FontColor = "1";  //1-红 2-绿 3-蓝 4-黄5-紫 6-青 7-白 8-黑，也可以是 RGB 表示的 颜 色 （ 如 红 色 ：FF0000）
        public string BackColor = "8";

        public int Effect = 0;  //0-无特效 1-立即显示2-上展 3-下展 4-左展5-右展 6-上移 7-下移8-左移 9-右移 10－淡入（入屏）淡出（出屏）255-随机 12-从右向左滚动 13-从左向有右滚动 14-从下往上滚动15-从上往下滚动
        public int EffectSpeed = 2;// 0-最慢 1-较慢 2-常速3-较快 4-最快
        public string Text = "";    //,、=、\\\应该为“\,”、“\=”、“\\”、“\\\\”
    }

    class TextItem : ItemBase
    {
        public string FontName = "1";    //1-黑 2-楷 3-宋 4-仿宋 5-隶书，也可以是字体名称（Arial 等）    
        public int FontSize = 16;    //各占两个字符。如：0909、1616、3232等
        public string FontColor = "1";  //1-红 2-绿 3-蓝 4-黄5-紫 6-青 7-白 8-黑，也可以是 RGB 表示的 颜 色 （ 如 红 色 ：FF0000）
        public string BackColor = "8";
        public string Flash = "0";
        public string Text = "";    //,、=、\\\应该为“\,”、“\=”、“\\”、“\\\\”
        public int FontStyle = 0;   //0-常规，1-加粗，2-倾斜，4-下划线，8-中划线（特效可以组合，如 3 表示字体加粗并且倾斜）
        public int FontSpace = 0;   //字间距 -1000-1000 0   
        public int FontDirection = 0;   //0横排 1纵排
        public int FlashDirection=0;
    }

    class ImageItem : ItemBase
    {
    }

    class GifItem : ItemBase
    {
        public string BackColor = "8";   //1-红 2-绿 3-蓝 4-黄5-紫 6-青 7-白 8-黑，也可以是 RGB 表示的 颜 色 （ 如 红 色 ：FF0000）
    }

    class VideoItem : ItemBase
    {

    }
    
}
